package com.example.happytime;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView jokeTv,recoTv,videoTv;
FragmentManager fragmentManager;
    FragmentTransaction ft;
    DrawerLayout drawerLayout;
List <Fragment> fList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        jokeTv=findViewById(R.id.left_tv_joke);
        recoTv=findViewById(R.id.left_tv_reco);
        videoTv=findViewById(R.id.left_tv_video);
        drawerLayout=findViewById(R.id.drawer);
        jokeTv.setOnClickListener(this);
        recoTv.setOnClickListener(this);
        videoTv.setOnClickListener(this);
        initFrag();
    }
    private void changeFrag(int pos){
        fragmentManager();
        for (int i=0;i<fList.size();i++){
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(pos));
        ft.commit();
        drawerLayout.closeDrawers();//点击关闭
    }
    //data初始化
    private void initFrag(){
        fList=new ArrayList<>();
        fList.add(new JokeFragment());
        fList.add(new RecoFragment());
        fList.add(new VideoFragment());
        fragmentManager();
        for (int i=0;i<fList.size();i++){
            ft.add(R.id.main_layout_center,fList.get(i));
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(0));
        ft.commit();

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_tv_joke:
                changeFrag(0);

                break;
            case R.id.left_tv_reco:
                changeFrag(1);

                break;
            case R.id.left_tv_video:
                changeFrag(2);
                break;

        }
    }
    public void fragmentManager(){
        fragmentManager=getSupportFragmentManager();//碎片管理者
         ft = fragmentManager.beginTransaction();//将碎片放到Fragment中
    }

}